﻿using UnityEngine;
using UnityEngine.UI;


/// <summary>
/// 资源更新组件
/// </summary>
public class UpdatePanel_UICtrl : UIControl
{
    [Header("UI组件")]
    public Text titleText;  // 标题文本，如"资源更新中"
    public Text statusText; // 状态信息，如"正在下载: characters.prefab"
    public Text progressText; // 进度百分比，如"75.5%"
    public Slider progressBar; // 进度条可视化
    public Text speedText; // 下载速度，如"1.5MB/s"
    public Text sizeText; // 文件大小，如"15.3MB/20.0MB"
    public Button actionButton; // 暂停/继续按钮
    public Text buttonText; // 按钮上的文字

    private float lastSpeedUpdateTime;
    private float lastDownloadedSize;
    private float downloadSpeed;

    public override void Awake()
    {
        base.Awake();

        titleText = this.viewDic["/UpdatePanelBgImage/titleText"].GetComponent<Text>();
        statusText = this.viewDic["/UpdatePanelBgImage/statusText"].GetComponent<Text>();
        progressText = this.viewDic["/UpdatePanelBgImage/progressText"].GetComponent<Text>();
        progressBar = this.viewDic["/UpdatePanelBgImage/progressBar"].GetComponent<Slider>();
        speedText = this.viewDic["/UpdatePanelBgImage/speedText"].GetComponent<Text>();
        sizeText = this.viewDic["/UpdatePanelBgImage/sizeText"].GetComponent<Text>();
        actionButton = this.viewDic["/UpdatePanelBgImage/actionButton"].GetComponent<Button>();
        buttonText = this.viewDic["/UpdatePanelBgImage/actionButton/buttonText"].GetComponent<Text>();
    }

    protected void OnShow()
    {
        titleText.text = "资源更新中";
        statusText.text = "准备更新...";
        progressBar.value = 0;
        progressText.text = "0%";
        speedText.text = "";
        sizeText.text = "";
        buttonText.text = "暂停";

        actionButton.onClick.RemoveAllListeners();
        actionButton.onClick.AddListener(OnPauseResumeClicked);
    }

    public void SetProgress(float progress, string status, long downloaded = 0, long total = 0)
    {
        progress = Mathf.Clamp01(progress);
        progressBar.value = progress;
        progressText.text = $"{progress * 100:F1}%";
        statusText.text = status;

        if (total > 0)
        {
            sizeText.text = $"{FormatSize(downloaded)} / {FormatSize(total)}";

            // 计算下载速度
            float currentTime = Time.time;
            if (currentTime - lastSpeedUpdateTime > 0.5f)
            {
                downloadSpeed = (downloaded - lastDownloadedSize) / (currentTime - lastSpeedUpdateTime);
                lastDownloadedSize = downloaded;
                lastSpeedUpdateTime = currentTime;
                speedText.text = $"{FormatSize((long)downloadSpeed)}/s";
            }
        }
    }

    public void UpdatePauseState(bool paused)
    {
        buttonText.text = paused ? "继续" : "暂停";
    }

    public void SetComplete()
    {
        titleText.text = "更新完成";
        statusText.text = "准备进入游戏";
        progressBar.value = 1;
        progressText.text = "100%";
        speedText.text = "";
        buttonText.text = "进入游戏";

        actionButton.onClick.RemoveAllListeners();
        actionButton.onClick.AddListener(OnEnterGameClicked);
    }

    public void SetFailed(string error)
    {
        titleText.text = "更新失败";
        statusText.text = error;
        buttonText.text = "重试";

        actionButton.onClick.RemoveAllListeners();
        actionButton.onClick.AddListener(OnRetryClicked);
    }

    private void OnPauseResumeClicked()
    {
        bool isPaused = buttonText.text == "继续";
        GameLanch.Instance.SetUpdatePaused(!isPaused);
    }

    private void OnRetryClicked()
    {
        GameLanch.Instance.RetryUpdate();
    }

    private void OnEnterGameClicked()
    {
        GameLanch.Instance.EnterGameAfterUpdate();
    }

    private string FormatSize(long bytes)
    {
        if (bytes < 1024) return $"{bytes}B";
        if (bytes < 1024 * 1024) return $"{(bytes / 1024f):F1}KB";
        if (bytes < 1024 * 1024 * 1024) return $"{(bytes / (1024f * 1024f)):F1}MB";
        return $"{(bytes / (1024f * 1024f * 1024f)):F1}GB";
    }
}